package UILayer;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowStateListener;
import java.awt.event.WindowEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.ListSelectionModel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.UIManager;

import ControllerLayer.IFTimetableController;
import ControllerLayer.TimetableController;
import ModelLayer.Hold;
import ModelLayer.HoldStatus;
import ModelLayer.Interval;
import ModelLayer.Lokalitet;
import ModelLayer.Medlem;
import ModelLayer.Tidsblok;

/**
 * 
 * @author DMAA0213 - Gruppe 2
 *
 */
public class Timetable extends JFrame {

	/**
	 * Start af application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIManager.setLookAndFeel(
							UIManager.getSystemLookAndFeelClassName());
					Timetable frame = new Timetable();
					frame.setExtendedState(MAXIMIZED_BOTH);
					frame.setVisible(true);
					//frame.setRow();
					frame.lavLogin();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	private Medlem medlemLoggetInd;
	private JPanel contentPane;
	private JTable table;
	private TimetableTableModel model;
	private JScrollPane scrollPane;
	private JMenuBar menuBar;
	private JMenu mnFile;
	private JMenu mnAdministration;
	private IFTimetableController timetableCtr;
	private ArrayList<Lokalitet> lokaliteter;
	private ArrayList<Hold> aktiveHold;

	/**
	 * Constructor til at lave vores hovedvindue.
	 */
	public Timetable() {
		timetableCtr = new TimetableController();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		menuBar = new JMenuBar();
		contentPane.add(menuBar, BorderLayout.NORTH);
		
		mnFile = new JMenu("File");
		menuBar.add(mnFile);
		
		mnAdministration = new JMenu("Administration");
		menuBar.add(mnAdministration);
		
		scrollPane = new JScrollPane();
		contentPane.add(scrollPane, BorderLayout.CENTER);
		
		try {
			lokaliteter = timetableCtr.hentListeLokaliteter();
			aktiveHold = timetableCtr.hentListeAktiveHold();
			//System.out.println(aktiveHold.size());
		} catch (SQLException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, "Fejl: " + e.getMessage());
			setVisible(false);
			
		}
		model = new TimetableTableModel(lokaliteter,aktiveHold);
		table = new JTable(model);
		HoldTextPaneCellRenderer h = new HoldTextPaneCellRenderer();
		h.saetLokaliteter(lokaliteter);
		for (int i=0;i<=Tidsblok.values().length; i++) {
			table.getColumnModel().getColumn(i).setCellRenderer(h);
		}
		table.setDefaultRenderer(Hold.class, h);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setRowSelectionAllowed(false);
		table.setFillsViewportHeight(true);
		table.setCellSelectionEnabled(true);
		table.setTableHeader(null);
		table.setRowHeight(88);
		model.fireTableDataChanged();

		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				klikMus();
			}
		});
		scrollPane.setViewportView(table);
	}
	
	/**
	 * Metode til at lave login.
	 */
	public void lavLogin() {
		LoginDialog j = new LoginDialog();
		j.setLocation((getWidth()/2)-(j.getWidth()/2), 
				(getHeight()/2)-(j.getHeight()/2));
		if(j.visModal()) {
			medlemLoggetInd = j.getMedlem();
			setTitle("Timetable - " + medlemLoggetInd.getNavn());
			model.SaetOpSorterMatrice();
		} else {
			setVisible(false);
		}
	}

	private void klikMus() {
		if (table.getSelectedRow() > 0 && table.getSelectedColumn() > 0) {
			Hold h = model.getHold(table.getSelectedRow(),table.getSelectedColumn());
			HoldDialog hd = new HoldDialog();
			if(h != null) {
				hd.setAdministrator(medlemLoggetInd.getAdministrator());
				hd.setHold(h);
				hd.setTitle("Vis Hold");
				hd.setLokaliteter(lokaliteter);
				hd.setLocation((getWidth()/2)-(hd.getWidth()/2), 
						(getHeight()/2)-(hd.getHeight()/2));
				hd.saetKnappetilstandVis();
				hd.visModal();
			} else {
				try {
					h = timetableCtr.opretHold(lokaliteter.get(table.getSelectedRow()-1)
							,Tidsblok.values()[table.getSelectedColumn()-1]);
					h.setInterval(Interval.UGENTLIG);
					h.setGamemaster(medlemLoggetInd);
					hd.setAdministrator(medlemLoggetInd.getAdministrator());
					hd.setHold(h);
					hd.tilladRette();
					hd.setTitle("Opret nyt hold");
					hd.setLokaliteter(lokaliteter);
					hd.setLocation((getWidth()/2)-(hd.getWidth()/2), 
							(getHeight()/2)-(hd.getHeight()/2));
					hd.saetKnaptilstandOpret();
					if (hd.visModal()) {
						//gem hold!
						timetableCtr.gemHold(h);
						aktiveHold.add(h);
						model.SaetOpSorterMatrice();
						
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}			
			}
		}
		table.clearSelection();
	}
}